# -*- coding: utf-8 -*-
#
# Copyright 2009 Vanderbilt University
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 

"""HORNET uses a event-driven plugin framework.  This module provides various
types of events and methods for validating those events.

.. moduleauthor:: John Paulett <john.paulett -at- vanderbilt.edu>
"""

class Event(object):
    """Base class for events."""
    def __init__(self, data=None, caller=None):
        self.data = data
        self._caller = caller
    
    @property
    def caller(self):
        return self._caller

    def __repr__(self):
        return '<%s caller="%s">' % (self.__class__.__name__, self.caller)
        
class GraphsReady(Event):
    """Event that indicates that a list of graphs is ready for sharing with
    other plugins. the *data* must be a list or iterator 
    of :class:`hornet.network.Graph` objects.
    """

class ProcessingStart(Event):
    pass
    
class EventNotAccepted(Exception):
    """Exception that is thrown when a plugin can not handle the type of a 
    given event.
    """
    
def is_accepted_event(event, accepted_types):
    """Determines if the *event* is one of the *accepted_types*. *event*
    should be an instance of :class:`hornet.event.Event` or instance of a 
    subclass of :class:`hornet.event.Event`.  *accepted_types* can be a single
    class or a tuple of classes.
    
    >>> e = GraphsReady(None)
    >>> is_accepted_event(e, (GraphsReady,))
    True
    """
    if accepted_types == None:
        return False
    return isinstance(event, accepted_types)

def graphs_ready(event):
    """Utility method for determining if *event* is a 
    :class:`hornet.event.GraphsReady` event.
    
    >>> e = GraphsReady(None)
    >>> graphs_ready(e)
    True
    """
    return is_accepted_event(event, GraphsReady)
        
